package n50_atomic;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.atomic.AtomicReference;

import static utils.Sleeper.sleep;

/**
 * @author fu
 * @date 2022-02-13 17:01
 * 概要：
 * ----------------------------------------------------------------
 * |                                                              |
 * |     主线程仅能判断出共享变量的值与最初值 A 是否相同，              |
 * |     不能感知到这种从 A 改为 B 又 改回 A 的情况                   |
 * |                                                             |
 * |==============================================================
 */
@Slf4j(topic = "c.T4")
public class T4_ABA问题 {


    static AtomicReference<String> ref = new AtomicReference<>("A");

    public static void main(String[] args) throws InterruptedException {
        log.debug("main start...");
        // 获取值 A
        // 这个共享变量被它线程修改过？
        String prev = ref.get();
        log.debug(prev);


        other();
        sleep(1);
        // 尝试改为 C
        log.debug("change A->C {}", ref.compareAndSet(prev, "C"));

        log.debug(ref.get());
    }

    private static void other() {
        new Thread(() -> {
            log.debug("change A->B {}", ref.compareAndSet(ref.get(), "B"));
        }, "t1").start();
        sleep(0.5);

        new Thread(() -> {
            log.debug("change B->A {}", ref.compareAndSet(ref.get(), "A"));
        }, "t2").start();
    }
}